using FirstTea.Application.DTOs;
using FirstTea.Application.Interfaces;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace FirstTea.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class TeaController : ControllerBase
    {
        private readonly ITeaService _teaService;
        public TeaController(ITeaService teaService)
        {
            _teaService = teaService;
        }
        [HttpGet]
        public async Task<ActionResult> GetAll()
        {
            var teas = await _teaService.GetAllAsync();
            return Ok(teas);
        }
            /// <summary>
            /// 分页获取茶叶列表
            /// </summary>
            [HttpGet("paged")]
            public async Task<ActionResult> GetPaged([FromQuery] int page = 1, [FromQuery] int pageSize = 10)
            {
                var teas = await _teaService.GetAllAsync();
                var paged = teas.Skip((page - 1) * pageSize).Take(pageSize);
                return Ok(paged);
            }
            /// <summary>
            /// 茶叶模糊搜索
            /// </summary>
            [HttpGet("search")]
            public async Task<ActionResult> Search([FromQuery] string keyword)
            {
                var teas = await _teaService.GetAllAsync();
                var result = teas.Where(t => t.Name != null && t.Name.Contains(keyword, StringComparison.OrdinalIgnoreCase));
                return Ok(result);
            }
        [HttpGet("{id}")]
        public async Task<ActionResult<TeaDto?>> GetById(Guid id)
        {
            var tea = await _teaService.GetByIdAsync(id);
            if (tea == null) return NotFound();
            return Ok(tea);
        }
        [HttpPost]
        public async Task<ActionResult> Create([FromBody] TeaDto dto)
        {
            await _teaService.CreateAsync(dto);
            return Ok();
        }
        [HttpPut]
        public async Task<ActionResult> Update([FromBody] TeaDto dto)
        {
            await _teaService.UpdateAsync(dto);
            return Ok();
        }
        [HttpDelete("{id}")]
        public async Task<ActionResult> Delete(Guid id)
        {
            await _teaService.DeleteAsync(id);
            return Ok();
        }
    }
}
